/**
 * 游戏界面
 * by wyy
 */
class gameScene extends GameObject implements eui.UIComponent {
    private static instance: gameScene;
    //游玩的区域
    public playGrp: eui.Group;
    //玩家
    public player: player;
    public playerGrp1: eui.Group;
    public playerGrp2: eui.Group;
    public playerGrp3: eui.Group;
    public playerGrp4: eui.Group;
    public playerGrp5: eui.Group;
    public playerGrp6: eui.Group;

    //方块
    public brickGrp: brickGrp;
    public brickShowGrp: eui.Group;

    //射击摇杆
    public shootPadGrp: eui.Group;
    private shootPad: shootPad;
    //子弹池
    public bulletPool: bulletPool;
    //边界墙的厚度
    public wallH = 10;
    //射击定时器
    public shootTimer: egret.Timer;

    public gameColor = {
        "white": 0xffffff,
        "green": 0x80ffff,
        "black": 0x000000
    }

    constructor() {
        super();
        this.skinName = "resource/eui_skins/game_skins/gameSceneSkin.exml";
    }

    childrenCreated() {
    }
    public static getInstance(): gameScene {
        (!this.instance) && (this.instance = new gameScene());
        return this.instance;
    }


    //初始化
    firstCreate() {
        physicsUtil.getInstance().createPhyWorld();
        //创建子弹池
        this.bulletPool = bulletPool.getInstance();

        let stageH = egret.MainContext.instance.stage.stageHeight;
        let stageW = egret.MainContext.instance.stage.stageWidth;
        //创建四周围栏
        this.createWall({ width: stageW, height: 20, position: [stageW / 2, 0] });
        this.createWall({ width: stageW, height: 20, position: [stageW / 2, stageH] });
        this.createWall({ width: 20, height: stageH, position: [stageW, stageH / 2] });
        this.createWall({ width: 20, height: stageH, position: [0, stageH / 2] });

        //创建玩家
        this.createPlayer();
        //创建射击摇杆
        this.createShootPad();
        //创建定时发射规则
        this.createShootTimer();
        //创建方块显示组件
        this.brickGrp = new brickGrp({});
        this.brickShowGrp.addChild(this.brickGrp);
        //创建碰撞规则
        physicsUtil.getInstance().phyWorld.on("endContact", this.contact, this);

    }
    //碰撞检测
    contact(r) {
        if ((r.bodyA.displays[0].type == "bullet") && (r.bodyB.displays[0].type == "bullet")) {
            return;
        }
        if (r.bodyA.displays[0].type == "bullet") {
            r.bodyA.displays[0].hited(r.bodyB.displays[0]);
        }
        if (r.bodyB.displays[0].type == "bullet") {
            r.bodyB.displays[0].hited(r.bodyA.displays[0]);
        }
    }
    addToStage() {

    }
    onRemove() {

    }
    //创建方形
    private createWall(obj) {
        let body = physicsUtil.getInstance().createWall(obj);
        this.playGrp.addChild(body.displays[0]);
    }
    //创建玩家
    private createPlayer(pos = 1, shootNum = 10) {
        this.player = new player(pos);
        this.player.anchorOffsetX = this.player.width / 2;
        this.player.anchorOffsetY = this.player.height / 2;
        this["playerGrp" + pos].addChild(this.player);
        this.player.x = this["playerGrp" + pos].width / 2;
        this.player.y = this["playerGrp" + pos].height / 2;
        this.player.shootNum = shootNum;
        //玩家移动控制
        this["playerGrp" + pos].addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.player.moveBegin, this.player);
        this.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.player.move, this.player);
        this.addEventListener(egret.TouchEvent.TOUCH_END, this.player.moveEnd, this.player);
        this.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, this.player.moveEnd, this.player);

    }
    //创建射击摇杆
    private createShootPad() {
        this.shootPad = new shootPad(this.player);
        this.shootPad.anchorOffsetX = this.shootPad.width / 2;
        this.shootPad.anchorOffsetY = this.shootPad.height / 2;
        this.shootPad.visible = false;
        this.shootPadGrp.addChild(this.shootPad);

        //点击空白部分出摇杆 
        this.shootPadGrp.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.shootPad.showShootPad, this.shootPad);
        this.shootPadGrp.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.shootPad.move, this.shootPad);
        this.shootPadGrp.addEventListener(egret.TouchEvent.TOUCH_END, this.shootPad.moveEnd, this.shootPad);
        this.shootPadGrp.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, this.shootPad.moveEnd, this.shootPad);
    }
    //创建子弹
    public createBullet(x, y) {
        let b = this.bulletPool.getBullet();
        this.playGrp.addChild(b);
        physicsUtil.getInstance().phyWorld.addBody(b.phyBody);
        b.phyBody.velocity = [x, y];
    }
    //射击
    public shoot() {
        if (!this.shootPad.bCanShoot) {
            return;
        }
        this.player.bMove = false;
        this.player.bShooting = true;
        this.timerShoot();
        this.shootTimer.start();
    }
    //定时器控制发射
    private timerShoot() {
        let x = -Math.cos(this.player.shootAngle) * this.player.shootForce;
        let y = Math.sin(this.player.shootAngle) * this.player.shootForce;
        this.createBullet(x, y);
    }
    //射击结束
    private endShoot() {
        this.player.bShooting = false;
        this.shootTimer.reset();
    }
    //射击定时器
    private createShootTimer() {
        this.shootTimer = new egret.Timer(100, this.player.shootNum - 1);
        this.shootTimer.addEventListener(egret.TimerEvent.TIMER, this.timerShoot, this);
        this.shootTimer.addEventListener(egret.TimerEvent.TIMER_COMPLETE, this.endShoot, this);
    }
}

enum gameColor {
    "white" = 0xffffff,
    "green" = 0x80ffff,
    "black" = 0x000000
}